﻿using Microsoft.Win32;
using SharpShell.Attributes;
using SharpShell.Registry;
using SharpShell.ServerRegistration;
using SharpShell.ServiceRegistry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tisn
{
    public static class Utility
    {
        /// <summary>
        /// resize bitmap with high quality
        /// </summary>
        /// <param name="original"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap ResizeBitmap(Bitmap original, Size size)
        {
            // Get better image while stretch
            if (original != null)
            {
                Bitmap b = new Bitmap(size.Width, size.Height);
                using (Graphics g = Graphics.FromImage((Image)b))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                    g.DrawImage(original, 0, 0, size.Width, size.Height);
                }
                return b;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// resize a rectangle bitmap
        /// </summary>
        /// <param name="orignial"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static Bitmap ResizeBitmap(Bitmap original, int width)
        {
            Size size = new Size();
            if (original.Width > original.Height)
            {
                size.Width = width;
                size.Height = (int)Math.Ceiling(width * 1.0 / original.Width * original.Height);
            }
            else
            {
                size.Height = width;
                size.Width = (int)Math.Ceiling(width * 1.0 / original.Height * original.Width);
            }
            return ResizeBitmap(original, size);
        }

        public static bool IsSameColor(Color a, Color b)
        {
            if (a.A != b.A)
                return false;
            if (a.R != b.R)
                return false;
            if (a.G != b.G)
                return false;
            if (a.B != b.B)
                return false;
            return true;
        }

        public static bool IsWhilteBitmap(Bitmap src)
        {
            for (int w = 0; w < src.Width; w++)
            {
                for (int h = 0; h < src.Height; h++)
                {
                    var color = src.GetPixel(w, h);
                    if (!IsSameColor(color, Color.White))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static byte[] HexStringToByteArray(string hex)
        {
            hex = hex.Replace(" ", "");
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }


        public static readonly Dictionary<string, string> CDR_Names = new Dictionary<string, string>() {
            { ".cdr", "Tisn.CorelDRAW.Graphics.cdr" },
            { ".cdt", "Tisn.CorelDRAW.Graphics.cdt" },
            { ".cmx", "Tisn.CorelDRAW.Graphics.cmx" },
        };

        public static readonly Dictionary<string, string> PSD_Names = new Dictionary<string, string>() {
            { ".psd", "Tisn.Photoshop.Image.psd" },
            { ".pdd", "Tisn.Photoshop.Image.pdd" },
            { ".psb", "Tisn.Photoshop.Image.psb" },
        };

        public static readonly Dictionary<string, string> PDF_Names = new Dictionary<string, string>() {
            { ".pdf", "Tisn.Portable.Document.Format" },
        };

        public static readonly Dictionary<string, string> Adobe_Names = new Dictionary<string, string>() {
            { ".ai", "Tisn.Adobe.illustrator.Graphics" },
            { ".eps", "Tisn.Adobe.Encapsulated.PostScript.Graphics" },
            { ".indd", "Tisn.Adobe.indesign.Graphics" },
        };

        public static void PrevReg(Type type, RegistrationType registrationType, Dictionary<string, string> Names)
        {
            var associationAttributes = type.GetCustomAttributes(typeof(COMServerAssociationAttribute), true)
                .OfType<COMServerAssociationAttribute>().ToList();

            var registry = ServiceRegistry.GetService<IRegistry>();

            //  Get the classes base key.            ;
            using (var classesKey = registrationType == RegistrationType.OS64Bit
                ? registry.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64) :
                registry.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
            {
                foreach (var associationAttribute in associationAttributes)
                {
                    foreach (var fileExtension in associationAttribute.Associations)
                    {
                        if (Names.ContainsKey(fileExtension))
                        {
                            using (var fileExtensionClassKey = classesKey.CreateSubKey(fileExtension, RegistryKeyPermissionCheck.ReadWriteSubTree))
                            {
                                if (string.IsNullOrEmpty(fileExtensionClassKey.GetValue(null) as string))
                                {
                                    fileExtensionClassKey.SetValue(null, Names[fileExtension]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
